const db = uniCloud.database();
const userTable = db.collection("uni-id-users")
const $ = db.command

//
async function getRed(ids,i=0){
	if(i >= ids.length){
		return ''
	}
	let res = await userTable.doc(ids[i]).field({
		inviter_uid: true,
		isWho: true
	}).get()
	if(res.data[0] && res.data[0].isWho){
		return res.data[0].isWho
	}
	await getRed(ids,i+1)
}

/**
 * 设置相应等级分润
 * @param {String} id   用户当前用户ID
 * @param {Number} type   分润类型 0 会员购买 1活动购买
 * @param {Number} price   支付金额
 * @returns
 */
async function setProfitSharing({
	id,
	price,
	orderid
} = {}) {
	console.log("setProfitSharing-two-one-orderid",orderid)
	let now = new Date().getTime() //获取当前时间戳
	let res = await userTable.doc(id).field({
		inviter_uid: true,
		isWho: true,
		level:true
	}).get()
	console.log("res+++", res)
	let ids = res.data[0].inviter_uid || []
	if (ids.length == 0) { //没有直接推荐人 直接返回，不需要分润
		return true
	}
	let isWho = res.data[0].isWho || await getRed(ids)
	// console.log("isWho++++",isWho)
	let profitObj = {
		uid: id, //利益来源方
		create_time: now, //分配时间
		orderid,//订单Id
		type:1
	}
	//查询直接推荐人,直接推荐人是普通身份，不管
	let addArr = []
	let upRes = await userTable.doc(ids[0]).field({
		level: true
	}).get()
	console.log("upRes+", upRes)
	if (!upRes.data[0] || upRes.data[0].level == 0) { //直接推荐人是普通人
		if (isWho) {
			let whoRes = await userTable.doc(isWho).field({
				inviter_uid: true,
				level: true
			}).get()
			if (whoRes.data[0].level == 2) { //间推是月老
				addArr.push(Object.assign({}, profitObj, {
					pid: isWho, //利益分配方,月老,联创
					price: price * 0.1, //分配利益金额
					commit: "月老间推奖励", //分配说明
				}))
				let wIds = whoRes.data[0].inviter_uid || []
				if (wIds && wIds[0]) {
					let res1 = await userTable.doc(wIds[0]).field({
						level: true
					}).get()
					if (res1.data[0] && res1.data[0].level == 3) {
						addArr.push(Object.assign({}, profitObj, {
							pid: wIds[0], //利益分配方,联创
							price: price * 0.1, //分配利益金额
							commit: "联创间推奖励", //分配说明
						}))
					}
				}
			} else if (whoRes.data[0].level == 3) { //间推是联创
				addArr.push(Object.assign({}, profitObj, {
					pid: isWho, //利益分配方,月老,联创
					price: price * 0.2, //分配利益金额
					commit: "联创间推奖励", //分配说明
				}))
			}
		}
	} else if (upRes.data[0].level == 1) { //直接推荐人是红娘
		addArr.push(Object.assign({}, profitObj, {
			pid: ids[0], //利益分配方
			price: price * 0.2, //分配利益金额
			commit: "直推奖励", //分配说明
		}))
		if (isWho) {
			let whoRes = await userTable.doc(isWho).field({
				inviter_uid: true,
				level: true
			}).get()
			if (whoRes.data[0].level == 2) { //间推是月老
				addArr.push(Object.assign({}, profitObj, {
					pid: isWho, //利益分配方,月老,联创
					price: price * 0.1, //分配利益金额
					commit: "月老间推奖励", //分配说明
				}))
				let wIds = whoRes.data[0].inviter_uid || []
				if (wIds && wIds[0]) {
					let res1 = await userTable.doc(wIds[0]).field({
						level: true
					}).get()
					if (res1.data[0] && res1.data[0].level == 3) {
						addArr.push(Object.assign({}, profitObj, {
							pid: wIds[0], //利益分配方,联创
							price: price * 0.1, //分配利益金额
							commit: "联创间推奖励", //分配说明
						}))
					}
				}
			} else if (whoRes.data[0].level == 3) { //间推是联创
				addArr.push(Object.assign({}, profitObj, {
					pid: isWho, //利益分配方,月老,联创
					price: price * 0.2, //分配利益金额
					commit: "联创间推奖励", //分配说明
				}))
			}

		}
	} else if (upRes.data[0].level == 2) { //推荐人是月老
		addArr.push(Object.assign({}, profitObj, {
			pid: ids[0], //利益分配方,月老
			price: price * 0.3, //分配利益金额
			commit: "月老直推奖励", //分配说明
		}))
		let whoRes = await userTable.doc(ids[0]).field({
			inviter_uid: true,
		}).get()
		let wIds = whoRes.data[0].inviter_uid || []
		if (wIds && wIds[0]) {
			let res1 = await userTable.doc(wIds[0]).field({
				level: true
			}).get()
			// console.log("idsss++++++++",res1)
			if (res1.data[0] && res1.data[0].level == 3) {
				addArr.push(Object.assign({}, profitObj, {
					pid: wIds[0], //利益分配方,联创
					price: price * 0.1, //分配利益金额
					commit: "联创间推奖励", //分配说明
				}))
			}
		}
	} else if (upRes.data[0].level == 3) { //推荐人是联创
		addArr.push(Object.assign({}, profitObj, {
			pid: ids[0], //利益分配方,月老
			price: price * 0.4, //分配利益金额
			commit: "联创直推奖励", //分配说明
		}))
	}
	// console.log("addArr+++++", addArr)
	return await setOrder(addArr)
}

async function setOrder(data) {
	console.log("setOrder==========",data)
	if(data.length == 0){
		return true
	}
	for(let i=0;i<data.length;i++){
		// console.log("i+++++",i)
		await db.collection('uni-id-users').doc(data[i].pid).update({
			money:$.inc(data[i].price),
			total_money:$.inc(data[i].price)
		})
	}
	console.log("dataOrder++", data)
	return await db.collection("wolf-profit-order").add(data)
}

module.exports = {
	setProfitSharing
}